package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.cdc.busi.service.ICdcOrdmastService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 医嘱项
 * 
 * @ClassName: CdcOrdmastController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcordmast")
public class CdcOrdmastController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcordmast/";

	@Autowired
	private ICdcOrdmastService cdcOrdmastService;
	

    /**
     * 跳转到医嘱项列表首页
     */
    @RequestMapping("")
    @Permission("cdcordmast:list")
    public String index() {
        return PREFIX + "cdcordmast.html";
    }
	
    /**
     * 跳转到新增医嘱项
     */
    @RequestMapping("/to_add")
    @Permission("cdcordmast:add")
    public String toAdd() {
        return PREFIX + "cdcordmast_add.html";
    }
	
	/**
     * 跳转到修改医嘱项
     */
    @RequestMapping("/to_update/{cdcOrdmastId}")
    @Permission("cdcordmast:update")
    public String toUpdate(@PathVariable Long cdcOrdmastId, Model model) {
        CdcOrdmastEntity cdcOrdmast = this.cdcOrdmastService.selectById(cdcOrdmastId);
        model.addAttribute("cdcOrdmast",cdcOrdmast);
        return PREFIX + "cdcordmast_edit.html";
    }

    /**
     * 获取医嘱项列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcordmast:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcOrdmastEntity> page = new PageFactory<CdcOrdmastEntity>().defaultPage();
        List<CdcOrdmastEntity> result = this.cdcOrdmastService.page(page,params);
        page.setRecords((List<CdcOrdmastEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增医嘱项
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcordmast:add")
    @BussinessLog(value = "新增医嘱项")
    public Object add(CdcOrdmastEntity cdcOrdmast) {
        if (CommonUtil.isOneEmpty(cdcOrdmast)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcOrdmast.insert();
        return R.ok();
    }

    /**
     * 删除医嘱项
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcordmast:delete")
    @BussinessLog(value = "删除医嘱项")
    public Object delete(@RequestParam Long cdcOrdmastId) {
	
        this.cdcOrdmastService.deleteById(cdcOrdmastId);

        return R.ok();
    }

    /**
     * 修改医嘱项
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcordmast:update")
    @BussinessLog(value = "修改医嘱项")
    public Object update(CdcOrdmastEntity cdcOrdmast) {
        if (CommonUtil.isOneEmpty(cdcOrdmast)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcOrdmastEntity old = this.cdcOrdmastService.selectById(cdcOrdmast.getId());

        old.setXcode(cdcOrdmast.getXcode());
        old.setCode(cdcOrdmast.getCode());
        old.setDescription(cdcOrdmast.getDescription());
        old.setDrugCommonName(cdcOrdmast.getDrugCommonName());
        old.setChemCommonName(cdcOrdmast.getChemCommonName());
        old.setBusOrderCateId(cdcOrdmast.getBusOrderCateId());
        old.setBusOrderSubcateId(cdcOrdmast.getBusOrderSubcateId());
        old.setBusOrderTypeId(cdcOrdmast.getBusOrderTypeId());
        old.setDataId(cdcOrdmast.getDataId());
        old.setIsActive(cdcOrdmast.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
